package main

import (
	"errors"
	"log"
	"math/rand"
)

var humanNames = []string{
	"Thomas",
	"Michael",
	"Mr. Bottle",
	"Thorsten",
	"Phil the Shrill",
	"Heikom",
	"Heikuhl"}

type Human struct {
	Creature
	Inventory
}

func NewHuman(name string, location *Place) *Human {

	return &Human{*NewCreature(NewItem(name, location), true), *NewInventory()}

}

func getRandonName() string {
	// todo: einen pool bauen, aus dem gezogen wird, damit es keine wiederholungen gibt
	return humanNames[rand.Intn(len(humanNames))]

}

func (this *Human) Take(item Portable) {
	log.Printf("Mensch '%s' will item '%s' nehmen...", this.Name(), item.Name())
	log.Printf("items an diesem platz: %v", len(this.location.items))
	for _, item := range this.location.items {
		log.Print(item.Name())
	}
	// todo: mehrere items nehmen

	this.Bag(item, 1)
	this.location.remove(item)
	return
}

// implements ItemCarrier
func (this *Human) drop(itemName string) error {
	log.Printf("Mensch '%s' will item '%s' ablegen...", this.Name(), itemName)
	log.Printf("items im inventory: %v", len(this.Inventory.items))
	for item, number := range this.Inventory.items {
		log.Printf("%d %s", number, item.Name())
	}
	item := this.Inventory.searchEntity(itemName)
	if item == nil {
		return errors.New("No item with this name found in inventory.")
	} else {
		log.Print("item gefunden, kann also abgelegt werden...")
	}
	// todo: mehrere items nehmen
	this.Unbag(item, 1)
	this.location.put(item)
	return nil

}

// implements ItemCarrier
func (this *Human) give(itemName string, person string) error {
	// todo: das alls in Inventory.String()
	log.Printf("Human '%s' wants to give item '%s' to '%s'.", this.Name(), itemName, person)
	log.Printf("inventory containing %d items: ", len(this.Inventory.items))
	for item, number := range this.Inventory.items {
		log.Printf("%d %s", number, item.Name())
	}
	item := this.Inventory.searchEntity(itemName)
	if item == nil {
		return errors.New("No item with this name found in inventory.")
	} else {
		log.Print("item gefunden, kann also gegeben werden werden...")
	}
	// todo: an wen? person finden
	this.Unbag(item, 1)

	return nil
}
